﻿#include <QSettings>
#include <QDebug>
#include "../common/QtAppMacro.h"

#include "ConfigHelper.h"
#include "DirHelper.h"

static const char *KEY_Init_IsInit = "Init/IsInit";
static const char *KEY_Init_Version = "Init/Version";
static const char *KEY_Init_Language = "Init/Language";

ConfigHelper *ConfigHelper::m_config = nullptr;
ConfigHelper *ConfigHelper::getInstance()
{
    if (!m_config) {
        m_config = new ConfigHelper(nullptr);
    }
    return m_config;
}

ConfigHelper::ConfigHelper(QObject *parent) : QObject(parent)
{
    DirHelper * handle = DirHelper::getInstance();
    m_setting = new QSettings(handle->configPath()+ "/" + "config.ini",QSettings::IniFormat);

    /* 首次初始化，更新程序后，此段应不执行，沿用老配置信息（如索引等） */
    if (m_setting->value(KEY_Init_IsInit,false).toBool() == false) {
        m_setting->setValue(KEY_Init_IsInit,true);

        m_setting->setValue(KEY_Init_Version,QT_COMPANY_APP_VERSION);

        m_setting->setValue(KEY_Init_Language,"CN");
    }

    /* 检查版本号并决定重设网址，场景：用户安装更新后，URL可能发生变化（为了适应内测版本） */
    if (m_setting->value(KEY_Init_Version) != QT_COMPANY_APP_VERSION) {
        m_setting->setValue(KEY_Init_Version,QT_COMPANY_APP_VERSION);
    }
}

QString ConfigHelper::getVersion()
{
    return QT_COMPANY_APP_VERSION;
}

QString ConfigHelper::getLanguage()
{
    return m_setting->value(KEY_Init_Language,"CN").toString();
}

void ConfigHelper::setLanguage(const QString &country)
{
    if (country == "CN" || country == "EN")
        m_setting->setValue(KEY_Init_Language,country);
}
